<?php
class PanelGroupMail extends PageElement
{
	private $allRecipients = array();
	
	public function addRecipient($recipient)
	{
		array_push($this->allRecipients,$recipient);
	}

		
	public function setRecipients($arrRecipients)
	{
		$this->allRecipients = $arrRecipients;
	}

	
	public function getHTML()
	{
		$html = "<h1>Group Email</h1>\n";
//		print_r($this->allRecipients);

		if(isset($_POST['SubAction'])&&($_POST['SubAction']=="SendMessage"))
		{
			$html .= "<div style=\"width:50%;margin-left:auto;margin-right:auto;\">\n";
		
			//			$html .= "send message - ".$_POST['TAMessage']." ".$_POST['TBSubject']."<br>\n";
			$recipients = array();
			$i=0;
			$message = new Message();
			$html .= "<p>Message sent to:<br>";
			foreach($this->allRecipients as $recipient)
			{
//				print_r($recipient);
//				echo "<br>\n";
				if($recipient['Email']=="")continue;
				if(isset($_POST['CBRecipient'.$i]))
				{
					if($_POST['CBRecipient'.$i]=='on')
					{
//						$html .= $recipient['Email']."<br>\n";
						$message->addRecipient($recipient['Email']);
						$html .= $recipient['Email']."<br>\n";
					}
				}
				
					$i++;
			}
			$message->addStyle("p, td {font-family: sans-serif;vertical-align:top;font-size: 70%;color:#003366;}");
			$message->setSubject($_POST['TBSubject']);
			$message->setFrom("SNAP message from ".$this->getParentPage()->getUserEmail()."<no-reply@stnicholascathedralmusic.co.uk>");
			$message->setReplyTo($this->getParentPage()->getUserEmail());
			$message->setMessageBody("<p>".nl2br($_POST['TAMessage'])."</p>");
			$message->send();
			
			$html .="</p></div>\n";
		}
		
		else
		{
			$hidden = new Hidden();
			$hidden->setName("SubAction");
			$html .= $hidden->getHTML();
			
			$html .= "<div style=\"width:50%;margin-left:auto;margin-right:auto;\">\n";
			$i=0;
			foreach($this->allRecipients as $recipient)
			{
				if($recipient['Email']=="")continue;
				$checkbox = new Checkbox();
				$checkbox->setName("CBRecipient".$i);
				$checkbox->setText($recipient['Name']."&nbsp;&nbsp;&nbsp;(".$recipient['Email'].")");
				$checkbox->setChecked(true);
				$html .= "<p>".$checkbox->getHTML()."</p>";
				$i++;
			}
			
			$textbox = new Textbox();
			$textbox->setName("TBSubject");
			$html .= "<p>Subject:<br>".$textbox->getHTML()."</p>\n";
				
			$textarea = new TextArea();
			$textarea->setName("TAMessage");
			$textarea->setRows("30");
			$textarea->setColumns("50");
			$html .= "<p>Message:<br>".$textarea->getHTML()."</p>\n";
			
			$html .= "<br>\n";
			
			$button = new JavascriptButton();
			$button->setJavascript("document.getElementById('SubAction').value = 'SendMessage';document.getElementById('PageForm').submit();");
			$button->setValue("Send to selected recipients");
			$html .= $button->getHTML();
			
			$html .= "</div>\n";
		}
		return $html;
		
	}
}
?>